﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BI.WebAPI.Models;

namespace BI.WebAPI.Controllers
{
    public class ContactsController : ApiController
    {
        private static List<Contact> contacts = new List<Contact> {
            new Contact{
               Id="001",
               Name ="张三",
               PhoneNo="123",
               EmailAddress="zhangsan@gmail.com"
            },
               new Contact{
               Id="002",
               Name ="李四",
               PhoneNo="456",
               EmailAddress="lisi@gmail.com"
            }
        };
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Contact> Get()
        {
            return contacts;
        }

        public Contact Get(string id)
        {
            return contacts.FirstOrDefault(c=>c.Id==id);
        }

        public void Put(Contact contact)
        {
            contact.Id = Guid.NewGuid().ToString();
            contacts.Add(contact);
        }

        public void Post(Contact contact)
        {
            Delete(contact.Id);
            contacts.Add(contact);
        }

        public void Delete(string id)
        {
            var contact = contacts.FirstOrDefault(c => c.Id == id);
            contacts.Remove(contact);
        }
    }
}
